package com.iss.supermarket.UserMangerUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.entity.SpUser;
import com.iss.supermarket.util.repository.UserDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class NewSpUserPanel extends JFrame{
    //单例模式
    private static NewSpUserPanel newSpUserPanel;
    //定义组件
    private JTextField[] editTexts;
    private JLabel[] editLabels;
    private JButton jButton1,jButton2;
    private JPanel returnPanel;
    private JComboBox<String> remarkComBox;
    private SpUser NewspUser;

    //连接数据库
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    UserDao userDao = ctx.getBean("UserDao", UserDao.class);

    public NewSpUserPanel(){
        super("编辑用户信息");

        InitAssembly();
        InitUI();
    }

    private void InitUI(){

        Container container = getContentPane();
        Box verticalBox = Box.createVerticalBox();

        for(int i=0;i<8;++i){
            JPanel tmpPanel = new JPanel();
            tmpPanel.add(editLabels[i]);
            if(i == 6){
                tmpPanel.add(remarkComBox);
            }else{
                tmpPanel.add(editTexts[i]);
            }
            verticalBox.add(tmpPanel);
        }

        JPanel jPanel1 = new JPanel();
        jPanel1.add(jButton1);
        jPanel1.add(jButton2);
        verticalBox.add(jPanel1);

        returnPanel.add(verticalBox);
        container.add(returnPanel);

        setSize(500,700);
        setComponentInCenter(this);
        setVisible(true);
    }

    private void InitAssembly(){
        //生成用户uuid和创建时间
        String uuid = UUID.randomUUID().toString();
        Date date=new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String modtime = format.format(date);

        editLabels = new JLabel[8];
        editLabels[0] = new JLabel("用户UUID");
        editLabels[0].setHorizontalAlignment(SwingConstants.CENTER);
        editLabels[0].setPreferredSize(new Dimension(50,25));

        editLabels[1] = new JLabel("用户名");
        editLabels[2] = new JLabel("用户密码");
        editLabels[3] = new JLabel("用户邮箱");
        editLabels[4] = new JLabel("用户手机号码");
        editLabels[5] = new JLabel("昵称");
        editLabels[6] = new JLabel("角色");
        editLabels[7] = new JLabel("创建时间");

        for(int i=0;i<editLabels.length;++i){
            editLabels[i].setHorizontalAlignment(SwingConstants.CENTER);
            editLabels[i].setPreferredSize(new Dimension(100,25));
        }

        editTexts = new JTextField[8];
        editTexts[0] = new JTextField(uuid);
        editTexts[0].setEditable(false);
        editTexts[1] = new JTextField();
        editTexts[2] = new JTextField();
        editTexts[3] = new JTextField();
        editTexts[4] = new JTextField();
        editTexts[5] = new JTextField();
        editTexts[7] = new JTextField(modtime);
        editTexts[7].setEditable(false);

        for(int i=0;i<editLabels.length;++i){
            if(i != 6){
                editTexts[i].setHorizontalAlignment(SwingConstants.CENTER);
                editTexts[i].setPreferredSize(new Dimension(300, 25));
            }
        }

        jButton1 = new JButton("保存");
        jButton1.addActionListener(new ButtonAction());
        jButton2 = new JButton("取消");
        jButton2.addActionListener(new ButtonAction());

        remarkComBox = new JComboBox<String>();
        remarkComBox.addItem("无");
        remarkComBox.addItem("管理员");
        remarkComBox.addItem("收银员");
        remarkComBox.addItem("仓库管理员");
        remarkComBox.addItem("售后管理员");
        remarkComBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    if("无".equals(e.getItem())){
                        remarkComBox.setSelectedIndex(-1);
                    }
                }
            }
        });
        remarkComBox.setSelectedIndex(-1);
        remarkComBox.setEditable(false);
        remarkComBox.setPreferredSize(new Dimension(300,25));

        returnPanel = new JPanel();
    }

    private class ButtonAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if("保存".equals(e.getActionCommand())){
                //单例模式访问类对象
                UserManagerList userManagerList = UserManagerList.getuserManagerList();
                JList<SpUser> jList = userManagerList.getjList();
                ListModelArray<SpUser> listModelArray = (ListModelArray<SpUser>) userManagerList.getjList().getModel();
                setNewspUser();

                boolean addflag = true;
                for(int i=0;i<listModelArray.getSize();++i){
                    if(NewspUser.getMobile().equals(listModelArray.getList().get(i).getMobile())){
                        addflag = false;
                    }
                }
                if(addflag){
                    userManagerList.flushList(NewspUser);
                    //除增加用户外的所有功能均通过update的方式更新数据库
                    //新用户如果没有插入数据库中，则虽然在listModelArray中但是不能使数据库中数据更新
                    userDao.AddNewSpUser(NewspUser.getUserUuid(),
                            NewspUser.getUserName(),
                            NewspUser.getPassword(),
                            NewspUser.getEmail(),
                            NewspUser.getMobile(),
                            NewspUser.getNiceName(),
                            NewspUser.getRemark(),
                            NewspUser.getModtime(),
                            0);
                }
                dispose();
            }else if("取消".equals(e.getActionCommand())){
                dispose();
            }
        }
    }

    public static synchronized NewSpUserPanel getNewSpUserPanel(){
        if(newSpUserPanel == null){
            newSpUserPanel = new NewSpUserPanel();
        }
        return newSpUserPanel;
    }

    public void setNewspUser(){
        //设置用户信息
        NewspUser = new SpUser();
        NewspUser.setUserUuid(editTexts[0].getText());
        NewspUser.setUserName(editTexts[1].getText());
        NewspUser.setPassword(editTexts[2].getText());
        NewspUser.setEmail(editTexts[3].getText());
        NewspUser.setMobile(editTexts[4].getText());
        NewspUser.setNiceName(editTexts[5].getText());
        NewspUser.setRemark(String.valueOf(remarkComBox.getSelectedItem()));
        NewspUser.setModtime(editTexts[7].getText());
    }

    private void setComponentInCenter(Component component){
        Dimension UISIZAE = component.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (UISIZAE.height > screenSize.height)
            UISIZAE.height = screenSize.height;
        if (UISIZAE.width > screenSize.width)
            UISIZAE.width = screenSize.width;
        component.setLocation((screenSize.width - UISIZAE.width) / 2, (screenSize.height - UISIZAE.height) / 2);
    }
}
